//
//  MTAsset.swift
//  TA
//
//  Created by M on 2019/12/11.
//  Copyright © 2019 m. All rights reserved.
//

import UIKit
import Photos

public class MTAsset: NSObject {
    
    /// 资源
    public var asset: PHAsset
    
    @objc
    public enum MediaType: Int {
        case image = 0
        case video, live, gif, audio
    }
    /// 媒体类型
    public var mediaType: MediaType {
        switch asset.mediaType {
        case .video: return .video
        case .audio: return .audio
        case .image:
            if (asset.value(forKey: "filename") as? String)?.hasSuffix("GIF") ?? false
                || (asset.value(forKey: "filename") as? String)?.hasSuffix("gif") ?? false {
                return .gif
            }
            return .image
        default: return .image
        }
    }
    
    /*
     * 视频相关
     */
    /// 视频资源
    public var videoAsset: AVAsset?
    /// 视频缓存地址
    public var videoPath: String?
    /// 视频时长：以秒为计算单位
    public var duration: TimeInterval {
        return asset.duration.rounded()
    }
    /// 视频时长
    public var timeLenght: String {
        return duration.transferTime
    }
    
    /*
     * iCloud 相关
     */
    /// 是否在云上
    public var isInCloud: Bool = false
    /// 下载进度
    public var progress: Double?

    /// 是否选中
    public var isSelected: Bool = false
    /// 是否可选
    public var isEnabled: Bool = true
    
    /// 初始化方法
    init(asset: PHAsset) {
        self.asset = asset
        super.init()
    }
    
}
